package cn.juforg.phoneshotgun;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.util.Log;
import android.widget.Toast;
import cn.juforg.guns.Gun;

public class MotionDetecter {
	public static final int NONE_GESTURE = 0;
	public static final int UP_GESTURE = 8;
	public static final int LEFT_GESTURE = 4;
	public static final int RIGHT_GESTURE = 6;
	public static final int DOWN_GESTURE = 2;
	
	
	private List<Integer> gestureList = new ArrayList<Integer>();
	private HashMap<String, Integer> motionMap;
	private Gun gun;
	
	
	private static  float x_last;
	private static  float y_last;
	private static  float z_last;
	
	public MotionDetecter(Gun gun) {
		super();
		this.motionMap = gun.motionMap;
		this.gun = gun;
	}
	
	/**
	 * 
	 * @param sensor
	 * @param context
	 * @return 返回动作key在motionMap对应的value
	 */
	public synchronized int detectMotion(SensorEvent sensor,Context context){
		int currentGesture = detectGesture(sensor, context);
		Log.i("phoneGun", "currentGesture:"+currentGesture);
		if ((currentGesture != NONE_GESTURE && gestureList.size() ==0) && (1==1 || gestureList.lastIndexOf(currentGesture)!=gestureList.size()-1)) {
			gestureList.add(currentGesture);
			StringBuilder sb = new StringBuilder();
			for (int e : gestureList) {
				sb.append(e);
			}
			int mot = gun.motionMap.get(sb.toString());
			gestureList.clear();
			Log.i("phoneGun", "mot:"+mot);
			return  mot;
		}
		
		return Gun.GunMotion.NO_MOTION;
	}
	
	/**
	 * 
	 * @param sensor
	 * @param context
	 * @return 返回手机运动方向
	 */
	public int detectGesture(SensorEvent sensor,Context context){

		if (null != sensor) {
			float x = sensor.values[SensorManager.DATA_X];
			float y = sensor.values[SensorManager.DATA_Y];
			float z = sensor.values[SensorManager.DATA_Z];
			this.gun.posture=this.detectPosture(x, y, z);
			if (x < 0){
				Toast.makeText(context, "x<0;y="+y+";y_last="+y_last+";z="+z,Toast.LENGTH_SHORT).show();
				if( y>SensorManager.GRAVITY_EARTH && y-y_last > 3 ) {
				Toast.makeText(context, "x:"+x+";y:"+y+";z:"+z,Toast.LENGTH_SHORT).show();
				return Constants.Gesture.UP_GESTURE;
			}}
			x_last = x;
			y_last = y;
			z_last = z;
		}
		return NONE_GESTURE;
	}
	
	public int detectPosture(float x, float y, float z){
		
		int posture = Constants.Posture.FLATWISE ;
		if (x>SensorManager.GRAVITY_EARTH) {
			posture = Constants.Posture.IN_THWART;
		}else if (y>SensorManager.GRAVITY_EARTH) {
			posture = Constants.Posture.UPRIGHT;
		}else if (z>SensorManager.GRAVITY_EARTH) {
			posture = Constants.Posture.FLATWISE;
		}else if (x<=SensorManager.GRAVITY_EARTH && y<=SensorManager.GRAVITY_EARTH && z<=SensorManager.GRAVITY_EARTH) {
			posture = Constants.Posture.IN_UPRIGHT;
		}else {
			
		}
		
		return posture;
	}
	
	public boolean isEarthGravity(int i){
		return Math.abs(i-SensorManager.GRAVITY_EARTH) <=Constants.ACCURACY_RANGE;
	}
	public boolean isNoGravity(int i){
		return Math.abs(i) <=Constants.ACCURACY_RANGE;
	}
	
}
